package com.woniuxy.springboot08rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayRabbitConfig {

    public static final String ORDER_QUEUE_NAME="user.order.queue";
    public static final String ORDER_EXCHANGE_NAME="user.order.exchange";
    public static final String ORDER_ROUTING_KEY="order";

    public static final String ORDER_DELAY_EXCHANGE="user.order.delay.exchange";
    public static final String ORDER_DELAY_QUEUE="user.order.delay.queue";
    public static final String ORDER_DELAY_ROUTING_KEY="order_delay";


    @Bean
    public Queue delayOrderQueue(){
        Map<String,Object> params=new HashMap<>(2);
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange",ORDER_EXCHANGE_NAME);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
        params.put("x-dead-letter-routing-key",ORDER_ROUTING_KEY);
        return new Queue(ORDER_DELAY_QUEUE,true,false,false,params);
    }
    @Bean
    public DirectExchange orderDelayExhangce(){
        return new DirectExchange(ORDER_DELAY_EXCHANGE);
    }

    @Bean
    public Binding dlxBinding(){
        return  BindingBuilder.bind(delayOrderQueue()).to(orderDelayExhangce()).with(ORDER_DELAY_ROUTING_KEY);
    }

    @Bean
    public Queue orderQueue(){
        return  new Queue(ORDER_QUEUE_NAME,true);
    }

    @Bean
    public TopicExchange orderTopicExchange(){
        return  new TopicExchange(ORDER_EXCHANGE_NAME);
    }
    
    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderTopicExchange()).with(ORDER_ROUTING_KEY);
    }
}
